# -*- coding: utf-8 -*-
import pygame
from pygame.locals import *

class RadioButton:
    def __init__(self, contenido, x, y):
        pass

class Bloque:
    def __init__(self, largo, ancho, left, top, seleccion):
        self.contenido = None
        self.img = pygame.Surface((largo, ancho))
        self.img.fill(pygame.Color("white"))
        self.rect = self.img.get_rect()
        self.rect.left = left
        self.rect.top = top
        self.font = pygame.font.Font(None, ancho)
        self.click = False
        self.seleccion = seleccion
    def set(self, perfil):
        if perfil != None:
            self.img.blit(self.font.render(perfil.id, 1, pygame.Color("black")), (0, 0))
        else:
            self.img.fill(pygame.Color("white"))
        self.contenido = perfil
        if self.seleccion.seleccionado == self: self.seleccionar()
        else: self.deseleccionar()
    def draw(self, surface):
        surface.blit(self.img, self.rect)
    def seleccionar(self):
        if self.contenido != None:
            self.img.fill(pygame.Color("black"))
            self.img.blit(self.font.render(self.contenido.id, 1, pygame.Color("white")), (0, 0))
    def deseleccionar(self):
        if self.contenido != None:
            self.img.fill(pygame.Color("white"))
            self.img.blit(self.font.render(self.contenido.id, 1, pygame.Color("black")), (0, 0))
    def on_event(self, event):
        if event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                if pygame.mouse.get_pos()[0] in range(self.rect.left, self.rect.right-40) and pygame.mouse.get_pos()[1] in range(self.rect.top, self.rect.bottom):
                    self.seleccion.set_seleccionado(self)

class Seleccion:
    def __init__(self):
        self.seleccionado = None
    def set_seleccionado(self, this):
        self.seleccionado = this

class ListBox:
    def __init__(self, lista, x, y):
        self.lista = lista
        self.boton_up = Boton("images/arrowup1.png", "images/arrowup2.png", x + 250, y + 15)
        self.boton_down = Boton("images/arrowdown1.png", "images/arrowdown2.png", x + 250, y + 75)
        self.seleccion = Seleccion()
        self.b_1 = Bloque(270, 30, x + 0, y + 0, self.seleccion)
        self.b_2 = Bloque(270, 30, x + 0, y + 30, self.seleccion)
        self.b_3 = Bloque(270, 30, x + 0, y + 60, self.seleccion)
        self.guia = 0
        self.col = pygame.Surface((40, 30))
        self.col.fill(pygame.Color("black"))
    def draw(self, surface):
        if len(self.lista) > self.guia and self.guia >= 0:
            self.b_1.set(self.lista[self.guia])
        if len(self.lista) > self.guia + 1:
            self.b_2.set(self.lista[self.guia + 1])
        if len(self.lista) > self.guia + 2:
            self.b_3.set(self.lista[self.guia + 2])
        self.b_1.draw(surface)
        self.b_2.draw(surface)
        self.b_3.draw(surface)
        if len(self.lista) > 3:
            self.boton_up.draw(surface)
            self.boton_down.draw(surface)
            surface.blit(self.col, (self.boton_up.rect.left, self.boton_up.rect.bottom))
    def on_event(self, event):
        self.boton_up.on_event(event)
        self.boton_down.on_event(event)
        self.b_1.on_event(event)
        self.b_2.on_event(event)
        self.b_3.on_event(event)
        if self.boton_up.is_clickeado():
            if self.guia > 0:
                self.guia -= 1
            self.boton_up.clickeado = False
        if self.boton_down.is_clickeado():
            if self.guia + 3 < len(self.lista):
                self.guia += 1
            self.boton_down.clickeado = False
    def borrar(self, perfil):
        self.lista.remove(perfil)
        self.b_1.set(None)
        self.b_2.set(None)
        self.b_3.set(None)

class Text:
    def __init__(self, FontName=None, FontSize=30):
        self.font = pygame.font.Font(FontName, FontSize)
        self.size = FontSize
        self.text = ""
        self.largo = 0
    def concat(self, letra):
        if self.largo < 14:
            self.largo += 1
            self.text += letra
    def set(self, palabra):
        self.text = palabra
    def delete(self):
        self.text = ""
        self.largo = 0
    def borrar(self):
        self.text = self.text[0:len(self.text)-1]
        self.largo -= 1
    def render(self, surface, color, pos):
        text = unicode(self.text, "UTF-8")
        surface.blit(self.font.render(text, 1, color), pos)
    def on_event(self, event):
        if event.type == KEYDOWN:
            if event.key == K_a: self.concat("a")
            if event.key == K_b: self.concat("b")
            if event.key == K_c: self.concat("c")
            if event.key == K_d: self.concat("d")
            if event.key == K_e: self.concat("e")
            if event.key == K_f: self.concat("f")
            if event.key == K_g: self.concat("g")
            if event.key == K_h: self.concat("h")
            if event.key == K_i: self.concat("i")
            if event.key == K_j: self.concat("j")
            if event.key == K_k: self.concat("k")
            if event.key == K_l: self.concat("l")
            if event.key == K_m: self.concat("m")
            if event.key == K_n: self.concat("n")
            if event.key == 241: self.concat("ñ")
            if event.key == K_o: self.concat("o")
            if event.key == K_p: self.concat("p")
            if event.key == K_q: self.concat("q")
            if event.key == K_r: self.concat("r")
            if event.key == K_s: self.concat("s")
            if event.key == K_t: self.concat("t")
            if event.key == K_u: self.concat("u")
            if event.key == K_v: self.concat("v")
            if event.key == K_w: self.concat("w")
            if event.key == K_x: self.concat("x")
            if event.key == K_y: self.concat("y")
            if event.key == K_q: self.concat("z")
            if event.key == K_0 or event.key == 256: self.concat("0")
            if event.key == K_1 or event.key == 257: self.concat("1")
            if event.key == K_2 or event.key == 258: self.concat("2")
            if event.key == K_3 or event.key == 259: self.concat("3")
            if event.key == K_4 or event.key == 260: self.concat("4")
            if event.key == K_5 or event.key == 261: self.concat("5")
            if event.key == K_6 or event.key == 262: self.concat("6")
            if event.key == K_7 or event.key == 263: self.concat("7")
            if event.key == K_8 or event.key == 264: self.concat("8")
            if event.key == K_9 or event.key == 265: self.concat("9")
            if event.key == K_SPACE: self.concat(" ")
            if event.key == K_BACKSPACE: self.borrar()

class Boton:
    def __init__(self, img_nopress, img_press, posx, posy):
        self.img_press = img_press
        self.img_nopress = img_nopress
        self.img = pygame.image.load(self.img_nopress)
        self.rect = self.img.get_rect()
        self.rect.centerx = posx
        self.rect.centery = posy
        self.clickeado = False
    def draw(self, surface):
        surface.blit(self.img, self.rect)
    def on_event(self, event):
        if event.type == MOUSEBUTTONDOWN:
            if event.button == 1:
                if pygame.mouse.get_pos()[0] in range(self.rect.left, self.rect.right) and pygame.mouse.get_pos()[1] in range(self.rect.top, self.rect.bottom):
                    self.img = pygame.image.load(self.img_press)
        if event.type == MOUSEBUTTONUP:
            if event.button == 1:
                self.img = pygame.image.load(self.img_nopress)
                if pygame.mouse.get_pos()[0] in range(self.rect.left, self.rect.right) and pygame.mouse.get_pos()[1] in range(self.rect.top, self.rect.bottom):
                    self.clickeado = True
    def is_clickeado(self):
        return self.clickeado